<template>
    <view>
        <!--  -->
        <form v-if="user.shiming == 0 && loading == true" class="padding-xs">
            <!--   		<view class="cu-form-group margin-top">
      	<view class="title">真实姓名</view>
      	<input :enableNative="false" placeholder="请输入真实姓名" v-model="username"></input>
      </view>
      <view class="cu-form-group">
      	<view class="title">身份证号</view>
      	<input :enableNative="false" placeholder="请输入身份证号码" type="idcard"  v-model="idcard"></input>
      </view>
      <view class="padding ">
      	<text class="text-gray">
      	请输入与您真实的姓名和身份证号，需要通过百度人脸识别是否与本人一致
      </text>
      </view> -->
            <view class="flex flex-direction padding">
                <view class="margin-xs flex" style="align-items: center">
                    <checkbox-group @change="RadioChange" style="display: inline-block">
                        <checkbox :class="radio == 'A' ? 'checked' : ''" :checked="radio == 'A' ? true : false" value="A"></checkbox>
                    </checkbox-group>
                    <text class="text-sm margin-left-sm">
                        您需对注册信息的真实性、合法性、有效性承担全部责任；{{ app.title }}账户
                        <text class="text-red">仅限本人使用</text>
                        ，不得冒用他人信息进行注册，否在本平台有权立即停止向您提供服务并保留追究法律责任的权利
                    </text>
                </view>
            </view>
            <view class="padding flex flex-direction">
                <!-- #ifdef APP -->
                <button class="cu-btn bg-red margin-tb-sm lg" :disabled="radio == 'A' ? false : true" @click="namerenz('app')" style="width: 90%; margin: auto">开始认证</button>
                <!-- #endif -->
                <!-- #ifndef APP -->
                <button class="cu-btn bg-red margin-tb-sm lg" :disabled="radio == 'A' ? false : true" @tap="namerenz" style="width: 90%; margin: auto">开始认证</button>
                <!-- #endif -->
            </view>
            <!-- #ifndef MP-WEIXIN -->
            <!--  		<view
  v-if="aliPayUrl"
  class="url-box" style="text-align: center;">
  	<view>
  		复制认证地址使用浏览器打开：
  	</view>
  	<view class="url">
  		<text> {{ aliPayUrl }}</text>
  	</view>
  	<view @click="copyUrl">
  		<button class="cu-btn bg-red margin-tb-sm lg">复制</button>
  	</view>
  </view> -->
            <!-- #endif -->
        </form>
        <view class="" v-else-if="user.shiming > 0 && loading == true">
            <view class="">
                <view class="padding-xs flex align-center">
                    <view class="flex-sub text-center margin-top">
                        <view class="text-xsl padding round bg-blue" style="width: 320rpx; margin: auto">
                            <image :src="errorImageUrl" style="width: 260rpx; height: 230rpx" mode="aspectFill"></image>
                        </view>
                        <view class="padding text-green">您经通过实名认证</view>
                    </view>
                </view>
            </view>
            <view class="bg-white margin" style="border: 1px solid #f1f1f1; border-radius: 8px">
                <view class="flex solid-bottom padding-sm justify-between">
                    <view class="padding-sm">真实姓名</view>
                    <view class="padding-sm">{{ user.userReal.name }}</view>
                </view>
                <view class="flex solid-bottom padding-sm justify-between">
                    <view class="padding-sm">身份证号</view>
                    <view class="padding-sm">{{ user.userReal.idcard }}</view>
                </view>
                <view class="flex solid-bottom padding-sm justify-between">
                    <view class="padding-sm">绑定手机</view>
                    <view class="padding-sm">{{ user.mobile }}</view>
                </view>
                <view class="flex solid-bottom padding-sm justify-between">
                    <view class="padding-sm">认证日期</view>
                    <view class="padding-sm">{{ user.userReal.updatetime }}</view>
                </view>
                <view class="flex solid-bottom padding-sm justify-between">
                    <view class="padding-sm">认证方式</view>
                    <view class="padding-sm">百度实名认证</view>
                </view>
            </view>
            <view class="padding">
                <button class="cu-btn bg-blue lg" style="width: 100%" @tap="goToUrl('enterprise')">升级企业认证</button>
            </view>
        </view>
    </view>
</template>
<script>
export default {
    data() {
        return {
            radio: null,
            username: null,
            idcard: null,
            loading: true,
            user: [],
            app: this.app,
            aliPayUrl: '' //网页链接
        };
    },
    onLoad() {
        uni.showLoading({
            title: '加载中...',
            mask: true
        });
        var that = this;
        this.getUser();
    },
    computed: {
        errorImageUrl() {
            return this.app.httpUrl + '/static/uniapp/img/resl.png';
        }
    },
    methods: {
        copyUrl() {
            uni.setClipboardData({
                data: this.aliPayUrl,
                success: () => {
                    uni.showToast({
                        title: '复制成功',
                        icon: 'none'
                    });
                }
            });
        },
        goToUrl(e) {
            uni.navigateTo({
                url: e
            });
        },
        RadioChange: function (e) {
            this.radio = e.detail.value[0];
            console.log(e.detail.value, this.radio);
        },
        getUser() {
            var that = this;
            uni.request({
                url: that.app.apiHost + '/mindex',
                method: 'POST',
                header: { Authorization: 'bearer ' + that.app.getAuthCode() },
                success: (res) => {
                    console.log(res.data);
                    if (res.data.code == -1) {
                        that.app.showMsg('请登陆后再操作!', '../login/login');
                    }
                    if (res.data.code == 0) {
                        uni.setStorageSync('users', JSON.stringify(res.data.data));
                        that.user = res.data.data;
                        uni.hideLoading();
                        that.loading = true;
                    }
                },
                fail(res) {
                    console.log(res);
                }
            });
        },
        parseString(queryString) {
            const params = {};
            const queries = queryString.split('&');
            queries.forEach((query) => {
                const [key, value] = query.split('=');
                params[decodeURIComponent(key)] = decodeURIComponent(value) || ''; // 使用uni-app提供的decodeURIComponent，或自定义实现
            });
            return params;
        },
        // 百度实名认证
        namerenz(value) {
            var that = this;
            // if (this.username == '' || this.username == null) {
            //   that.app.showMsg('请输入真实姓名');
            //   return;
            // }
            // if (this.idcard == '' || this.idcard == null) {
            //   that.app.showMsg('请输入身份证号码');
            //   return;
            // }
            uni.showLoading({
                title: '通讯中...',
                mask: true
            });
            var iswx = 0;
            let resurl = 'cardurl://affimresult?order=';
            uni.request({
                url: that.app.apiHost + '/faceAuth',
                method: 'POST',
                header: { Authorization: 'bearer ' + that.app.getAuthCode() },
                success: (res) => {
                    uni.hideLoading();
                    console.log(res.data.data.H5Url);
                    this.aliPayUrl = res.data.data.H5Url;
                    let encodedUrl = encodeURIComponent(this.aliPayUrl);
                    if (value == 'app') {
                        plus.runtime.openURL(res.data.data.H5Url);
                    } else {
                        uni.navigateTo({
                            url: '/pages/common/webgzh?url=' + encodedUrl
                        });
                    }
                },
                fail(res) {
                    console.log(res);
                }
            });
            // 如果不是小程序走这里
            // // #ifndef MP-WEIXIN
            // uni.request({
            //   url: that.app.apiHost + '/accindex',
            //   method: 'POST',
            //   header: { Authorization: 'bearer ' + that.app.getAuthCode() },
            //   data: { username: that.username, idcard: that.idcard, iswx: iswx, backurl: resurl },
            //   success: (res) => {
            //     if (res.data.code == -1) {
            //       that.app.showMsg('请登陆后再操作!', '../login/login');
            //     }
            //     if (res.data.code == 0) {
            //       this.aliPayUrl = res.data.alipayurl;
            //       uni.hideLoading();
            //       // #ifdef H5
            //       window.location.href = res.data.alipayurl;
            //       // #endif
            //       // #ifdef APP
            //       plus.runtime.openURL(res.data.alipayurl);
            //       uni.navigateTo({
            //         url: '../index/autoresult?order=' + res.data.orderno + '&type=load'
            //       });
            //       // #endif
            //     } else if (res.data.code == 3) {
            //       uni.navigateTo({
            //         url: '../index/aliapyaccount?id=' + res.data.alipayurl
            //       });
            //     } else {
            //       that.app.showMsg(res.data.msg);
            //     }
            //   },
            //   fail(res) {
            //     console.log(res);
            //   }
            // });
            // // #endif
        },
        // 支付宝实名认证
        authsub() {
            var that = this;
            // if (this.username == '' || this.username == null) {
            //   that.app.showMsg('请输入真实姓名');
            //   return;
            // }
            // if (this.idcard == '' || this.idcard == null) {
            //   that.app.showMsg('请输入身份证号码');
            //   return;
            // }
            uni.showLoading({
                title: '通讯中...',
                mask: true
            });
            // var iswx=this.isweixin()
            var iswx = 0;
            let resurl = 'cardurl://affimresult?order=';
            uni.request({
                url: that.app.apiHost + '/accindex',
                method: 'POST',
                header: { Authorization: 'bearer ' + that.app.getAuthCode() },
                data: { username: that.username, idcard: that.idcard, iswx: iswx, backurl: resurl },
                success: (res) => {
                    console.log(res.data);
                    if (res.data.code == -1) {
                        that.app.showMsg('请登陆后再操作!', '../login/login');
                    }
                    if (res.data.code == 0) {
                        this.aliPayUrl = res.data.alipayurl;
                        // #ifdef MP-WEIXIN
                        // let encodedUrl = encodeURIComponent(this.aliPayUrl)
                        // uni.navigateTo({
                        //   url: '/pages/common/webgzh?url=' + encodedUrl
                        // })
                        // #endif
                        uni.hideLoading();
                        // #ifdef H5
                        window.location.href = res.data.alipayurl;
                        // #endif
                        // #ifdef APP
                        plus.runtime.openURL(res.data.alipayurl);
                        uni.navigateTo({
                            url: '../index/autoresult?order=' + res.data.orderno + '&type=load'
                        });
                        // #endif
                    } else if (res.data.code == 3) {
                        uni.navigateTo({
                            url: '../index/aliapyaccount?id=' + res.data.alipayurl
                        });
                    } else {
                        that.app.showMsg(res.data.msg);
                    }
                },
                fail(res) {
                    console.log(res);
                }
            });
        },
        isweixin() {
            let userAgent = navigator.userAgent;
            if (userAgent.includes('MicroMessenger')) {
                return 1;
            } else {
                return 0;
            }
        }
    }
};
</script>

<style lang="scss">
.url-box {
    padding: 0 20rpx;
    align-items: center;
    .url {
        white-space: pre-wrap;
    }
}
</style>
